package brian.android.mvc.activity.config;

import android.content.Context;

@SuppressWarnings("unchecked")
public class Configuration {
	private static Class r;
	private static Class id;
	private static Class layout;
	private static Class string;
	
	public static Class getIdClass(Context applicationContext) throws ClassNotFoundException {
		if (id == null) {
			initClass(applicationContext.getPackageName());
		}
		return id;
	}
	
	public static Class getRClass(Context applicationContext) throws ClassNotFoundException {
		if (r == null) {
			initClass(applicationContext.getPackageName());
		}
		return r;
	}
	
	public static Class getLayoutClass(Context applicationContext) throws ClassNotFoundException {
		if (layout == null) {
			initClass(applicationContext.getPackageName());
		}
		return layout;
	}
	
	public static Class getStringClass(Context applicationContext) throws ClassNotFoundException {
		if (string == null) {
			initClass(applicationContext.getPackageName());
		}
		return string;
	}
	
	private static void initClass(String packageName) throws ClassNotFoundException {
		r = Class.forName(packageName + ".R");
		Class[] classes = r.getClasses();
		
		for (Class clazz : classes) {
			if ("id".equals(clazz.getSimpleName())) {
				id = clazz;
			} else if ("layout".equals(clazz.getSimpleName())) {
				layout = clazz;
			} else if ("string".equals(clazz.getSimpleName())) {
				string = clazz;
			}
		}
	}
}
